package com.farm.controller;
import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.opjo.Crops;
import com.farm.reception.CropsDate;
import com.farm.service.impl.CropsServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * 农作物增删改查
 */
@RestController
@RequestMapping(value = "/crops")
public class CropsController {

    @Autowired
    private CropsServiceImpl cropsService;
    /**
     * 分页+条件查询(name)
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result searchSelect(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        String name = (String) searchMap.get ( "name" );
        if (name!=null){
           Crops crops = cropsService.findByName ( name );
           return new Result ( true, StatusCode.ACCESSERROR, "查询成功", crops );
       }
        Page<Crops> cropsList = cropsService.selectPage ( page, size );
        PageResult pageResult= new PageResult ( cropsList.getTotal (),cropsList.getResult () );
        System.out.println (cropsList);
        if (cropsList!=null){
           return new Result ( true, StatusCode.ACCESSERROR, "查询成功", pageResult );
       }
        return new Result ( false, StatusCode.ERROR, "查询失败" );
    }



    /**
     * @param cropsDate
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CropsDate cropsDate) {
        //1.非空判断
        if (cropsDate != null) {
            //2.查询数据库是否已经存在该农作物
            Crops crops = cropsService.findByName ( cropsDate.getCrops ().getName () );
            if (crops!=null){
                return new Result ( false, StatusCode.ERROR, "添加农作物已经存在" );
            }
        }
        cropsService.add ( cropsDate );
        return new Result ( true, StatusCode.ACCESSERROR, "添加成功" );
    }

    /**
     * 根据农作物id查询
     * @param cropsId
     * @return
     */
    @RequestMapping("/findByCropsId/{cropsId}")
    public Crops findByCropsId(@PathVariable("cropsId") Integer cropsId){
        Crops crops= cropsService.findByCropsId(cropsId);
        return crops;
    }

    /**
     * 更新
     * 步骤：
     *  1.农作物数据的更新
     *  2.再进行指标中间表字段的更新
     *  3.再进行病虫害表字段的更新
     * @param cropsDate
     * @return 后期更新的话如果需要更改和指标中间表数据和 病虫中间表的数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody CropsDate cropsDate) {
        if (cropsDate==null&& cropsDate.getCrops ()==null){
            return new Result ( false, StatusCode.ERROR, "需要更新的数据不完整" );
        }
        cropsService.update (cropsDate);
        return new Result ( true, StatusCode.ACCESSERROR, "修改成功" );
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam int id) {
        try {
            cropsService.delete ( id );
            return new Result ( true, StatusCode.USEERROR, "删除成功" );
        }catch (Exception e){
        }
        return new Result ( false, StatusCode.USEERROR, "删除失败，请检查是否存在数据关联" );
    }
}
